package com.zhilianbao.leyaogo.view.widgets;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bql.convenientlog.CLog;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.utils.KeyboardUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;

/**
 * Author: hcy
 * Description:支付密码输入
 * Created: 2016/11/5 11:27
 */
public class PasswordView extends FrameLayout {

    @BindViews({R.id.iv_password1, R.id.iv_password2, R.id.iv_password3, R.id.iv_password4, R.id.iv_password5, R.id.iv_password6})
    public List<ImageView> mIvList;

    @BindView(R.id.et_null)
    public EditText mEditText;

    public PasswordView(Context context) {
        this(context,null);
    }

    public PasswordView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_pass_word,this,false);
        ButterKnife.bind(this,view);
        addView(view);
        mEditText.addTextChangedListener(mTextWatcher);
    }

    private TextWatcher mTextWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            String str =  mEditText.getText().toString();
            if(str.contains(" ")){
                str = str.replace(" ","");
                mEditText.setText(str);
            }else if(mEditText.getText().toString().length()>6){
                str = str.substring(0,6);
                mEditText.setText(str);
            }else {
                setTextValue(str);
            }
        }
    };

    /**
     * 改变显示个数
     * @param str
     */
    private void setTextValue(String str) {
        int len = str.length();
        for(int i=len;i<mIvList.size();i++){
            mIvList.get(i).setVisibility(View.GONE);
        }
        for(int i=0;i<len;i++){
            mIvList.get(i).setVisibility(View.VISIBLE);
        }
        if (len == 6) {
            CLog.i("hcy","回调");
            CLog.i("hcy","支付密码" + str);
            if (mListener != null) {
                mListener.onNumCompleted(str);
            }
            KeyboardUtils.hideKeyboard(getContext(), mEditText);
        }
    }

    public interface EditCompleteListener {
        void onNumCompleted(String num);
    }

    public EditCompleteListener mListener;

    /**
     * 完成输入6位监听接口
     * @param mListener
     */
    public void setEditCompleListener(EditCompleteListener mListener) {
        this.mListener = mListener;
    }

    public void clearSecurityEdit() {
        mEditText.setText("");
    }

    public String getSecurityPassworde() {
        return mEditText.getText().toString();
    }
}
